import { expect } from '@jest/globals';
import * as fse from 'fs-extra';
import path from 'path';

import { runHvigorAsync } from '../../common/common';
import {
  commonJson5Operation
} from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel, IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType, SignType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import {TestTaskOption } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
};

const taskConfig: TestTaskOption = {
  caseLevel: CaseLevel.L1,
  sign: SignType.Sign,
  ohpmInstall: IsEnable.DisAble,
};

/**
 *  entry依赖hsp，entry的build-profile.json5中添加targets，同步编译构建报错。
 *
 * @param {Project} project
 * @param {TestTaskOption} taskConfig
 * @param {ICaseRecord} record
 * @return res
 */
const testfun: TestCaseFun = async (project, taskConfig, record) => {
  // 1. 创建API10 stage 工程 ，添加sharelibrary模块，在entry的oh-package.json5文件中配置hsp依赖
  const projectPath = project.projectRootPath();

  // 2. entry的build-profile.json5中添加targets
  await commonJson5Operation(path.resolve(projectPath, 'oh-package.json5'), async (json5) => {
    json5.devDependencies = {'code_hsp_mini' : '1.0.0'};
  });

  // 3. 执行同步编译构建
  const commands: string[] = '--sync -p product=default --analyze=normal --parallel --incremental --daemon'.split(' ');
  const result = await runHvigorAsync(projectPath, commands);
  record.addStep('执行同步编译构建', result);
  expect(result.stdout).toContain('Finished ::init');

  await fse.existsSync(path.resolve(projectPath, 'oh_modules/code_hsp_mini/oh-package.json5'));

  const commandHap: string[] =
        '--mode module -p product=default assembleHap --analyze=normal --parallel --incremental --daemon'.split(' ');
  const resultHap = await runHvigorAsync(project.projectRootPath(), commandHap);
  record.addStep('执行同步编译构建', resultHap);
  expect(resultHap.stdout).toContain('BUILD SUCCESSFUL');

  const commandApp: string[] =
        '--mode project -p product=default assembleApp --analyze=normal --parallel --incremental --daemon'.split(' ');
  const resultApp = await runHvigorAsync(projectPath, commandApp);
  record.addStep('执行同步编译构建', resultApp);
  expect(resultApp.stdout).toContain('BUILD SUCCESSFUL');
};

TestCaseBuilder.builder()
  .caseName('OPENLAB_AIDE_ZOS_DevEco_Studio_Build_HspPackageManagement_04')
  .description('在dependencies配置远程hsp包，构建hap/app')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(taskConfig)
  .testFun(testfun)
  .load();
